package edu.cuit.campus.portal.service.bean;

import edu.cuit.campus.commons.MailSender;
import edu.cuit.campus.commons.StringUtils;
import edu.cuit.campus.commons.response.Result;
import edu.cuit.campus.entity.po.Student;
import edu.cuit.campus.portal.service.StudentService;
import edu.cuit.campus.repository.common.MybatisHelper;
import edu.cuit.campus.repository.mapper.StudentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/7 15:21
 */
public class StudentServiceBean implements StudentService {
    private static final Logger log = LoggerFactory.getLogger(StudentServiceBean.class);

    @Override
    public Result register(Student student) {
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(StudentMapper.class);
        //1. 用户名不存在情况
        if(!mapper.existsByUsername(student.getUsername())){
            //对密码进行加密
            student.setPassword(StringUtils.hash(student.getPassword()));
            mapper.insert(student);
            session.commit();
        }
        MybatisHelper.close();
        //注册成功后,需要发送激活邮件
        if(student.getId() != null) {
            //发送邮件
            MailSender.sendHtmlEmail("账户激活", readMailContent(student), "成信大-校园招聘", student.getEmail());
            return Result.ok(null, "注册成功,请前往" + student.getEmail() + "进行账号激活");
        }
        return Result.fail("注册失败,用户名已经存在...");
    }

    //读取html模板内容
    private String readMailContent(Student student) {
        String content = "";
        try(var reader = new BufferedReader(new InputStreamReader(StudentServiceBean.class.getClassLoader().getResourceAsStream("mailContent.html")))){
            String line = null;
            while((line = reader.readLine()) != null)
                content += line;
        }catch (IOException e){
            log.error(e.getMessage(), e);
        }
        return content.replace("${user.username}", student.getUsername())
                .replace("${user.id}", student.getId() + "");
    }

    @Override
    public boolean existsByUsername(String username) {
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(StudentMapper.class);
        var result = mapper.existsByUsername(username);
        MybatisHelper.close();
        return result;
    }

    @Override
    public Result login(String username, String password) {
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(StudentMapper.class);
        var student = mapper.findByUsername(username);
        MybatisHelper.close();
        if(student == null)
            return Result.fail("用户不存在");
        else if(!StringUtils.check(password, student.getPassword()))
            return Result.fail("用户名或者密码错误");
        else if(!student.isStatus())
            return Result.fail("账户未激活,请联系管理员或者登录注册邮箱完成账户激活");
        return Result.ok(student);
    }

    @Override
    public boolean active(String username, Integer code) {
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(StudentMapper.class);
        var result = mapper.updateStatus(username, code);
        session.commit();
        MybatisHelper.close();
        return result == 1;
    }
}
